package com.lgx.common.tree;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.WrapDynaBean;
import org.apache.commons.lang.StringUtils;

import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.web.json.JsonUtils;

/**
 * 树
 * 
 * @author acer
 * 
 */
public class TreeUtils {

	/**
	 * 
	 * 
	 * @param dataList 数据源
	 * @param fields Tree需要的字段
	 * @param targets Tree对应数据源的字段
	 * @param list 父节点中记录子数据的集合标签，默认children
	 * @param orderField 排序标记，默认layerOrder
	 * @return
	 */
	public static String toJSONChangeObjectToTreeModelAddChildren(List<?> dataList,String[] fields,String[] targets,String list,String orderField)
	{
		JsonUtils json = new JsonUtils();
		return json.converToJson(changeObjectToTreeModelAddChildren(dataList,fields,targets,list,orderField));
	}
	
	/**
	 * 
	 * 
	 * @param dataList 数据源
	 * @param fields Tree需要的字段
	 * @param targets Tree对应数据源的字段
	 * @param list 父节点中记录子数据的集合标签，默认children
	 * @param orderField 排序标记，默认layerOrder
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<TreeModel> changeObjectToTreeModelAddChildren(List<?> dataList,String[] fields,String[] targets,String list,String orderField)
	{
		List<TreeModel> models = changeObjectToTreeModel(dataList,fields,targets);
		return (List<TreeModel>) addChildren(models,list,orderField);
	}
	
	/**
	 * 将对象集转换到TreeModel集中
	 * 
	 * @param dataList 数据源
	 * @param fields Tree需要的字段
	 * @param targets Tree对应数据源的字段
	 * @return
	 */
	public static List<TreeModel> changeObjectToTreeModel(List<?> dataList,String[] fields,String[] targets)
	{
		List<TreeModel> models = new ArrayList<TreeModel>();
		for(int i=0; i < dataList.size();i++)
		{
			WrapDynaBean dynaBean = new WrapDynaBean(dataList.get(i));
			TreeModel model = new TreeModel();
			WrapDynaBean _dynaBean = new WrapDynaBean(model);
			for(int j=0;j < targets.length ; j++)
			{
				if(containsField(dynaBean,fields[j]) && containsField(_dynaBean,targets[j]))
				{
					_dynaBean.set(targets[j], dynaBean.get(fields[j]).toString());
				}
			}
			models.add((TreeModel) _dynaBean.getInstance());
		}
		return models;
	}
	
	private static boolean containsField(WrapDynaBean dynaBean,String field)
	{
		if(dynaBean == null) return false;
		Field[] fs = dynaBean.getInstance().getClass().getDeclaredFields();
		for(Field f : fs)
		{
			if(field.equals(f.getName()))
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 将子节点加载到父节点中toJSON
	 * 
	 * @param dataList 数据源，按orderField排序
	 * @param list 父节点中记录子数据的集合标签，默认children
	 * @param orderField 排序标记，默认layerOrder
	 * @return
	 */
	public static String toJSONaddChildren(List<?> dataList,String list,String orderField)
	{
		JsonUtils json = new JsonUtils();
		return json.converToJson(addChildren(dataList,list,orderField));
	}
	
	/**
	 * 将子节点加载到父节点中
	 * 
	 * @param dataList 数据源，按orderField排序
	 * @param list 父节点中记录子数据的集合标签，默认children
	 * @param orderField 排序标记，默认layerOrder
	 * @return
	 */
	public static List<?> addChildren(List<?> dataList,String list,String orderField)
	{
		if(StringUtils.isEmpty(list)) list = "children";
		if(StringUtils.isEmpty(orderField)) orderField = "layerOrder";
		
		//结果数据集
		List<Object> datas = new ArrayList<Object>();
		//记录子数据编号
		List<Integer> dels = new ArrayList<Integer>();
		
		//根据layerOder关系将子加入父
		for(int i=dataList.size()-1;i>=0;i--)
		{
			WrapDynaBean dynaBean = new WrapDynaBean(dataList.get(i));
			String order = dynaBean.get(orderField).toString();
			for(int j=i-1;j>=0;j--)
			{
				WrapDynaBean _dynaBean = new WrapDynaBean(dataList.get(j));
				String _order = _dynaBean.get(orderField).toString();
				@SuppressWarnings("unchecked")
				List<Object> children = (List<Object>) _dynaBean.get(list);
				if(order.startsWith(_order))
				{
					children.add(dataList.get(i));
					dels.add(i);
					break;
				}
				
			}
		}
		
		//将父节点数据加入结果数据
		for(int i=0; i < dataList.size() ; i++)
		{
			if(!dels.contains(i))
			{
				datas.add(dataList.get(i));
			}
		}
		
		sort(datas,list,orderField);
		
		return datas;
	}
	
	private static void sort(List<?> dataList,String list,String orderField)
	{
		for(int i=0; i < dataList.size() ; i++)
		{
			WrapDynaBean dynaBean = new WrapDynaBean(dataList.get(i));
			@SuppressWarnings("unchecked")
			List<Object> children = (List<Object>) dynaBean.get(list);
			if(children != null)
			{
				sort(children,list,orderField);
			}
		}
		
		final String _orderField = orderField;
		
		Collections.sort(dataList, new Comparator<Object>(){

			@Override
			public int compare(Object o1, Object o2) {
				WrapDynaBean dynaBean = new WrapDynaBean(o1);
				WrapDynaBean _dynaBean = new WrapDynaBean(o2);
				String order = dynaBean.get(_orderField).toString();
				String _order = _dynaBean.get(_orderField).toString();
				return order.compareTo(_order);
			}});
	}
}
